package org.pixelami.twisted.internet.base
{
	public class BaseConnector
	{
		public var timeoutID = null;
	    public var factoryStarted = 0;
	    
	    public var host;
	    public var port;
	    public var bindAddress;
	    public var state;
		public var reactor;
		public var factory;
		public var timeout;
		public var transport;
		
		
		public function BaseConnector(host, port, factory, timeout, bindAddress, reactor=null)
		{
			this.state = "disconnected"
	        this.reactor = reactor
	        this.factory = factory
	        this.timeout = timeout
		}
		
		public function  disconnect()
		{
	        /*"""Disconnect whatever our state is."""*/
	        if (this.state == 'connecting')
	            this.stopConnecting()
 	        else if (this.state == 'connected')
 	            this.transport.loseConnection()
 		}
 	
 	    public function  connect()
 	    {
 	        /*"""Start connection to remote server."""*/
 	        if (this.state != "disconnected")
 	            throw new Error("can't connect in this state")
 	
 	        this.state = "connecting";
	        if (! this.factoryStarted)
	        {
 	            this.factory.doStart();
 	            this.factoryStarted = 1;
 	       }
 	        this.transport = transport = this._makeTransport()
 	        if (this.timeout != null)
 	        {
 	            var err = new Error('TimeoutError');
 	            //this.timeoutID = this.reactor.callLater(this.timeout, transport.failIfNotConnected, error.TimeoutError());
 	            this.timeoutID = this.reactor.callLater(this.timeout, transport.failIfNotConnected, err);
 	        }
 	        this.factory.startedConnecting(this)
 	    }
 	
 	    public function  stopConnecting()
 	    {
 	        /*"""Stop attempting to connect."""*/
 	        if (this.state != "connecting")
 	            //throw new NotConnectingError("we're not trying to connect");
 	            throw new Error("we're not trying to connect");
	
 	        this.state = "disconnected";
 	        var err = new Error('UserError');
 	        this.transport.failIfNotConnected(err)
 	        this.transport = null;
 	    }
 	
 	    public function cancelTimeout()
 	    {
 	        if(this.timeoutID != null)
 	        {
 	            try
 	            {
 	                this.timeoutID.cancel();
 	            }
 	            //catch(e:ValueError)
 	            catch(e:Error)
 	            {
 	                //pass
 	                //trace("cancelTimeout  - "+e)
 	            }
 	            this.timeoutID = null;
 	        }
 	    }
 	
 	    public function  buildProtocol(addr)
 	    {
 	        this.state = "connected";
 	        this.cancelTimeout();
 	        return this.factory.buildProtocol(addr);
 	    }
 	
 	    public function  connectionFailed(reason)
 	    {
 	        this.cancelTimeout();
 	        this.transport = null;
 	        this.state = "disconnected";
 	        this.factory.clientConnectionFailed(this, reason);
 	        if (this.state == "disconnected")
 	        {
 	            //# factory hasn't called our connect() method
 	            this.factory.doStop();
 	            this.factoryStarted = 0;
 	        }
 	    }
 	
 	    public function  connectionLost(reason)
 	    {
 	        this.state = "disconnected";
 	        this.factory.clientConnectionLost(this, reason);
 	        if (this.state == "disconnected")
 	        {
 	            //# factory hasn't called our connect() method
 	            this.factory.doStop();
 	            this.factoryStarted = 0;
 	        }
 	    }
 	
 	    public function  getDestination()
 	    {
 	        //throw new NotImplementedError("implement in subclasses");
 	        throw new Error("BaseConnector: implement in subclasses");
 	    }
 	    
 	    public function _makeTransport(){} // override

	}
}
